﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Radiation.Common;
using System.IO;

namespace RadiationUI.Forms
{
    public partial class RealtimeLog : Form
    {
        private static bool _opened = false;

        public static bool CanBeOpened
        {
            get
            {
                return !_opened;
            }
        }

        // dynamic part

        int m_out_size = 0;
        int m_in_size = 0;

        public RealtimeLog()
        {
            InitializeComponent();
        }

        private void fermerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void RealtimeLog_Load(object sender, EventArgs e)
        {
            _opened = true;

            Connection.NetworkDataReceived += new EventHandler<Radiation.Common.GenericEventArgs>(Connection_NetworkActive);
            Connection.NetworkDataSent += new EventHandler<GenericEventArgs>(Connection_NetworkDataSent);
            Logger.Instance["error"].ItemAdded += new EventHandler<GenericEventArgs>(RealtimeLog_ItemAdded);
            Logger.Instance["warning"].ItemAdded +=new EventHandler<GenericEventArgs>(RealtimeLog_ItemAddedWarning);
        }

        void Connection_NetworkDataSent(object sender, GenericEventArgs e)
        {
            MethodInvoker action = delegate
            {
                lock (listBox3)
                {
                    listBox3.Items.Add(DateTime.UtcNow.ToString() + " -- " + e["message"]);
                    listBox3.SelectedIndex = listBox3.Items.Count - 1;

                    labelCmdOut.Text = Convert.ToString(listBox3.Items.Count);
                    m_out_size += ((string)e["message"]).Length;
                    labelSizeOut.Text = Convert.ToString(m_out_size) + "bytes";
                }
            };
            this.BeginInvoke(action);
        }

        void RealtimeLog_ItemAdded(object sender, GenericEventArgs e)
        {
            MethodInvoker action = delegate
            {
                lock (listBox2)
                {
                    listBox2.Items.Add(DateTime.UtcNow.ToString() + " -- " + e["message"]);
                    listBox2.SelectedIndex = listBox2.Items.Count - 1;
                }
            };
            this.BeginInvoke(action);
        }

        void RealtimeLog_ItemAddedWarning(object sender, GenericEventArgs e)
        {
            MethodInvoker action = delegate
            {
                lock (listBox4)
                {
                    listBox4.Items.Add(e["message"]);
                    listBox4.SelectedIndex = listBox4.Items.Count - 1;
                }
            };
            this.BeginInvoke(action);
        }

        void Connection_NetworkActive(object sender, Radiation.Common.GenericEventArgs e)
        {
            MethodInvoker action = delegate
            {
                lock (listBox1)
                {
                    listBox1.Items.Add(e["message"]);
                    listBox1.SelectedIndex = listBox1.Items.Count - 1;

                    labelCmdIn.Text = Convert.ToString(listBox1.Items.Count);
                    m_in_size += ((string)e["message"]).Length;
                    labelSizeIn.Text = Convert.ToString(m_in_size) + "bytes";
                }
            };
            this.BeginInvoke(action);
        }

        private void RealtimeLog_FormClosing(object sender, FormClosingEventArgs e)
        {
            Connection.NetworkDataReceived -= new EventHandler<Radiation.Common.GenericEventArgs>(Connection_NetworkActive);
            Connection.NetworkDataSent -= new EventHandler<GenericEventArgs>(Connection_NetworkDataSent);
            Logger.Instance["error"].ItemAdded -= new EventHandler<GenericEventArgs>(RealtimeLog_ItemAdded);
            Logger.Instance["warning"].ItemAdded -= new EventHandler<GenericEventArgs>(RealtimeLog_ItemAddedWarning);

            _opened = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            lock (listBox1)
            {
                listBox1.Items.Clear();
            }
            lock (listBox3)
            {
                listBox3.Items.Clear();
            }
            m_in_size = 0;
            m_out_size = 0;
        }

        private void saveAll_Click(object sender, EventArgs e)
        {
            if (listBox1.Items.Count == 0 && listBox3.Items.Count == 0)
                MessageBox.Show("Il n'y a rien a enregistrer", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else
            {
                SaveFileDialog save_dlg = new SaveFileDialog();
                if (save_dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    try
                    {
                        StreamWriter sw = new StreamWriter(save_dlg.FileName, false);
                        sw.WriteLine("Radiation IM - Network capture\r\n");
                        sw.WriteLine("Incoming data : ");
                        sw.WriteLine("-------------------------------------------------");

                        foreach (string item in listBox1.Items)
                            sw.WriteLine(item);

                        sw.WriteLine("-------------------------------------------------");
                        sw.WriteLine("\r\nOutgoing data : ");
                        sw.WriteLine("-------------------------------------------------");

                        foreach (string item in listBox3.Items)
                            sw.WriteLine(item);

                        sw.WriteLine("-------------------------------------------------");
                        sw.WriteLine("END of network capture");

                        sw.Close();
                    }
                    catch (Exception ee)
                    {
                        MessageBox.Show(ee.ToString());
                    }
                }
            }
        }

        private void RealtimeLog_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                this.Close();
        }
    }
}
